<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :title="title"
    :visible="visible"
    :width="800"
    :dataSource="dataSource"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-divider orientation="left" style="font-weight: 500;margin-top: 0px;">基础信息</a-divider>
      <a-form-model ref="forms" :label-col="labelCol" :model="form" :rules="rules" :wrapper-col="wrapperCol" style="height: 100px"
      >
        <a-col :md="12" :sm="24">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="打卡地点名称"
            prop="address">
            <a-input v-model="form.address" :disabled="disableSubmit" placeholder=""/>
          </a-form-model-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="打卡范围(m)"
            prop="distance">
            <a-input v-model="form.distance" :disabled="disableSubmit" placeholder=""/>
          </a-form-model-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="打卡类型"
            prop="dataType">
            <a-select
              v-model="form.dataTypeStr"
              :disabled="disableSubmit"
              placeholder="请选择"
              @change="onchange"
            >
              <a-select-option value="1" :disabled="dataSource.find(v=>v.dataType==3||v.dataType==1)">
                上班签到
              </a-select-option>
              <a-select-option value="2" :disabled="dataSource.find(v=>v.dataType==3||v.dataType==2)">
                下班签退
              </a-select-option>
              <a-select-option value="3" :disabled="dataSource.find(v=>v.dataType==1||v.dataType==2||v.dataType==3)">
                上下班打卡
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-form-model>
      <t-map  v-if="visible" ref="ref-maps" :distance="form.distance"></t-map>

    </a-spin>
  </j-modal>
</template>

<script>
import TMap from '@comp/cn-components/TMap'
export default {
  name: 'PunchInPlaceModal',
  components: { TMap },
  props: {
    dataSource: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      form: {
        address: '', //地点名称
        distance: 300, //打卡范围
        dataType: '', //打卡类型
        dataTypeStr: ''//打卡类型文字说明
      },
      dataSource: [],
      // forms:{
      //   address:'', //地点名称
      //   distance:'', //打卡范围
      //   dataType:'', //打卡类型
      // },
      rules: {
        address: [{ required: true, message: ' ', trigger: ['change', 'blur'] }],
        distance: [{ required: true, message: ' ', trigger: ['change', 'blur'] }],
        dataType: [{ required: true, message: ' ', trigger: ['change', 'blur'] }]
      },
      disableSubmit: false,
      title: '操作',
      visible: false,
      labelCol: {
        xs: { span: 24, },
        sm: { span: 8, },
      },
      wrapperCol: {
        xs: { span: 24, },
        sm: { span: 16 },
      },
      confirmLoading: false,
    }
  },

  methods: {

    // 打卡类型赋值
    onchange(e) {
      this.form.dataType = Number(e)
      console.log(this.form.dataType, this.form.dataTypeStr)
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
      this.form = {
        address: '', //地点名称
        distance: 300, //打卡范围
        dataType: '', //打卡类型
        dataTypeStr: ''//打卡类型文字说明
      }
    },
    handleOk() {
      this.$refs.forms.validate(valid => {
        console.log(valid)
        if (valid) {
          let key = this.form.dataType
          let item = this.dataSource.find(item => item.dataType == key)
          let reg = /^(([1-9]{1}\d*)|(0{1}))((.\d{0,2})?)$/
          if (!reg.test(this.form.distance)) {
            this.$message.warning('请输入正确的打卡范围')
            return
          }
          if (item) {
            this.$message.warning('打卡类型已存在')

          } else {
            if (this.form.dataType === 1) {
              this.form.dataTypeStr = '上班签到'
              console.log(this.form.dataType, this.form.dataTypeStr)
            }
            if (this.form.dataType === 2) {
              this.form.dataTypeStr = '下班签退'
            }
            if (this.form.dataType === 3) {
              this.form.dataTypeStr = '上下班打卡'
            }
            this.$emit('ok', this.form)
            console.log(this.form)
            this.close()
            this.dataSource = []
          }
        } else {

        }
      })
    }
  },
}
</script>

<style scoped>

</style>